package com.qiyue.admin.controller.base;

import com.qiyue.admin.biz.base.GroupBiz;
import com.qiyue.admin.constant.AdminCommonConstant;
import com.qiyue.admin.entity.base.group.Group;
import com.qiyue.admin.vo.AuthorityMenuTree;
import com.qiyue.admin.vo.GroupTree;
import com.qiyue.admin.vo.GroupUsers;
import com.qiyue.common.controller.BaseController;
import com.qiyue.common.utils.TreeUtil;
import com.qiyue.utils.ResultUtils;
import com.qiyue.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by y747718944 on 2018/2/8
 */
@RestController
@RequestMapping("group")
@Api(description = "群组模块")
public class GroupController extends BaseController<GroupBiz,Group> {

    @GetMapping("/{id}/user")
    @ApiOperation("获取群组关联用户")
    public Result<GroupUsers> getGroupUsers(@PathVariable Long id){
        GroupUsers users=baseBiz.getGroupUsers(id);
        System.err.println(users.getLeaders().size());
        return ResultUtils.getResult(users);
    }

    @PutMapping("/{id}/user")
    @ApiOperation(" 变更群组所分配用户")
    public Result modifiyGroupUsers(@PathVariable Long id, String members, String leaders){

        baseBiz.modifyGroupUsers(id, members, leaders);
        return ResultUtils.success();
    }


    @GetMapping("/{id}/authority/menu")
    @ApiOperation("根据群组id 获取群组关联的菜单")
    public Result<List<AuthorityMenuTree>> getMenuAuthority(@PathVariable Long id){
        return ResultUtils.getResult(baseBiz.getAuthorityMenu(id));
    }

    @PostMapping("/{id}/authority/menu")
    @ApiOperation(" 变更群组关联的菜单")
    public Result modifyMenuAuthority(@PathVariable  Long id, String menuTrees){
        String [] menus = menuTrees.split(",");
        baseBiz.modifyAuthorityMenu(id,menus);
        return  ResultUtils.success();
    }

   @GetMapping("/{id}/authority/element")
   @ApiOperation("根据groupId 获取有权限的主键")
   public Result<List<Long>> getElementAuthority(@PathVariable Long id){
        return ResultUtils.getResult(baseBiz.getAuthorityElement(id));
   }

    @PostMapping("/{id}/authority/element")
    @ApiOperation("新增资源权限")
    public Result addElementAuthority(@PathVariable Long id,Long menuId, Long elementId){
        baseBiz.modifyAuthorityElement(id, menuId, elementId);
        return ResultUtils.success();
    }

    @DeleteMapping("/{id}/authority/element")
    @ApiOperation(" 移除主键权限")
    public Result removeElementAuthority(@PathVariable Long id,Long menuId, Long elementId){
        baseBiz.removeAuthorityElement(id, menuId, elementId);
        return ResultUtils.success();
    }



    @GetMapping("/tree")
    @ApiOperation("模糊匹配 群组名")
    public List<GroupTree> tree(String name, String groupType) {
        if(StringUtils.isBlank(name)&&StringUtils.isBlank(groupType)) {
            return new ArrayList<GroupTree>();
        }
        Map<String,Object> params=new HashMap<>();
        if (StringUtils.isNotBlank(name)) {
            params.put("LIKE_name",name);
        }
        if (StringUtils.isNotBlank(groupType)) {
            params.put("EQ_groupType",groupType);
        }
        return  getTree(baseBiz.findList(params), AdminCommonConstant.ROOT);
    }


    private List<GroupTree> getTree(List<Group> groups,Long root) {
        List<GroupTree> trees = new ArrayList<GroupTree>();
        GroupTree node = null;
        for (Group group : groups) {
            node = new GroupTree();
            node.setLabel(group.getName());
            BeanUtils.copyProperties(group, node);
            trees.add(node);
        }
        return TreeUtil.bulid(trees,root) ;
    }




}
